package com.zgjkhis.modules.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.zgjkhis.common.service.impl.BaseServiceImpl;
import com.zgjkhis.config.RedisKey;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.system.domain.Dict;
import com.zgjkhis.modules.system.domain.DictDetail;
import com.zgjkhis.modules.system.service.DictDetailService;
import com.zgjkhis.modules.system.service.DictService;
import com.zgjkhis.modules.system.service.dto.DictDetailDto;
import com.zgjkhis.modules.system.service.dto.DictDetailQueryCriteria;
import com.zgjkhis.modules.system.service.mapper.DictDetailMapper;
import com.zgjkhis.modules.util.CharUtil;
import com.zgjkhis.utils.FileUtil;
import com.zgjkhis.utils.RedisUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @date 2020-05-14
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "dictDetail")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DictDetailServiceImpl extends BaseServiceImpl<DictDetailMapper, DictDetail> implements DictDetailService {

    @Resource
    private final RedisUtils redisUtils;
    @Resource
    private final DictService dictService;

    @Override
    //@Cacheable
    public PageResult<DictDetailDto> queryAll(DictDetailQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<DictDetail> page = new PageInfo<>(queryAll(criteria));
        return PageResult.data(page, DictDetailDto.class);
    }


    @Override
    //@Cacheable
    public List<DictDetail> queryAll(DictDetailQueryCriteria criteria){
        List<DictDetail> list =  baseMapper.selectDictDetailList(criteria.getLabel(),criteria.getDictName());
        return list;
    }


    @Override
    public void download(List<DictDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DictDetailDto dictDetail : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("字典标签", dictDetail.getLabel());
            map.put("字典值", dictDetail.getValue());
            map.put("排序", dictDetail.getSort());
            map.put("字典id", dictDetail.getDictId());
            map.put("创建日期", dictDetail.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public String queryProcessCost(String key) {
        Object o = redisUtils.get(RedisKey.PROCESS_COST_KEY+key + LoginUserUtil.getPharmId());
        if(CharUtil.isEmpty(o)) {
            Dict dict = dictService.getOne(Wrappers.<Dict>query().lambda().eq(Dict::getName, key));
            DictDetail dictDetail = baseMapper.selectOne(Wrappers.<DictDetail>query().lambda().eq(DictDetail::getDictId, dict.getId()));
            redisUtils.set(RedisKey.PROCESS_COST_KEY+key + LoginUserUtil.getPharmId(),dictDetail.getValue());
            return CharUtil.isEmpty(dictDetail.getValue())?"0":dictDetail.getValue();
        }else{
            return o.toString();
        }
    }
}
